"""实现固定前两列table widget"""
import sys

# pylint: disable=W0614,W0401
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Fixtable(QTableWidget):
    """固定前两列的表"""
    def __init__(self, parent=None):
        super(Fixtable, self).__init__(parent)
        self.setWindowTitle("表格 demo")
        self.resize(600, 250)
        self.setRowCount(10)
        self.setColumnCount(50)

        for i in range(self.rowCount()):
            for j in range(self.columnCount()):
                self.setItem(i, j, QTableWidgetItem(str(i+j)))
        stre = "选到"
        self.setHorizontalHeaderLabels(["选刀","选到",stre])

        
        self.tablewidget_1 = QTableWidget(self)
        self.tablewidget_1.setHorizontalHeaderLabels(["选刀"])
        self.tablewidget_1.setRowCount(10)
        self.tablewidget_1.setColumnCount(2)
        for i in range(self.rowCount()):
            for j in range(self.columnCount()):
                self.tablewidget_1.setItem(i, j, QTableWidgetItem(str(i+j)))

        self.tablewidget_1.setFocusPolicy(Qt.NoFocus)
        self.tablewidget_1.verticalHeader().hide()
        self.tablewidget_1.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
        self.viewport().stackUnder(self.tablewidget_1)

        self.tablewidget_1.setSelectionModel(self.selectionModel())

        self.tablewidget_1.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.tablewidget_1.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.update_frozen_table_geometry()
        self.tablewidget_1.show()

        self.tablewidget_1.verticalScrollBar().valueChanged.connect(
            self.verticalScrollBar().setValue)
        self.verticalScrollBar().valueChanged.connect(
            self.tablewidget_1.verticalScrollBar().setValue)
        self.verticalScrollBar().valueChanged.connect(self.update_frozen_table_geometry)
        self.horizontalHeader().sectionResized.connect(self.update_section_width)
        self.verticalHeader().sectionResized.connect(self.update_section_height)

    def update_frozen_table_geometry(self):
        """更新坐标"""
        print(self.viewport().x(), self.viewport().y(), self.viewport().width(), self.viewport().height())
        self.tablewidget_1.setGeometry(
            self.verticalHeader().width() + self.frameWidth(),
            0,
            self.columnWidth(0) + self.columnWidth(1),
            self.viewport().height() + self.horizontalHeader().height()+2)

    # pylint: disable=W0613
    def update_section_width(self, old_size, new_size):
        """更新宽度"""
        self.tablewidget_1.setColumnWidth(0, new_size)
        self.tablewidget_1.setColumnWidth(1, new_size)
        self.update_frozen_table_geometry()

    # pylint: disable=W0613
    def update_section_height(self, logical_index, old_size, new_size):
        """更新高度"""
        self.tablewidget_1.setRowHeight(logical_index, new_size)


if __name__ == '__main__':
    APP = QApplication(sys.argv)
    TABLE = Fixtable()
    TABLE.show()
    APP.exit(APP.exec_())
